﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CMSService.Tools
{
    /// <summary>
    /// 分页条
    /// </summary>
    public class PageBar
    {
        /// <summary>
        /// 获得页码显示链接 1 2 3 4 5 6这种
        /// </summary>
        /// <param name="curPage">当前页数</param>
        /// <param name="countPage">总页数</param>
        /// <param name="url">超级链接地址(/ChildForum.aspx)</param>
        /// <param name="extendPage">周边页码显示个数上限(6)</param>
        /// <param name="pagetag">页码标记(pgid)</param>
        /// <returns>页码html</returns>
        public static string ToString(int curPage, int countPage, string controllerName, int extendPage)
        {
            int startPage = 1;
            int endPage = 1;

            string t1 = "<a href=\"/" + controllerName + "/1";
            string t2 = "<a href=\"/" + controllerName + "/" + countPage;

            t1 += "\">&laquo;</a>";
            t2 += "\">&raquo;</a>";

            if (countPage < 1)
                countPage = 1;
            if (extendPage < 3)
                extendPage = 2;

            if (countPage > extendPage)
            {
                if (curPage - (extendPage / 2) > 0)
                {
                    if (curPage + (extendPage / 2) < countPage)
                    {
                        startPage = curPage - (extendPage / 2);
                        endPage = startPage + extendPage - 1;
                    }
                    else
                    {
                        endPage = countPage;
                        startPage = endPage - extendPage + 1;
                        t2 = "";
                    }
                }
                else
                {
                    endPage = extendPage;
                    t1 = "";
                }
            }
            else
            {
                startPage = 1;
                endPage = countPage;
                t1 = "";
                t2 = "";
            }

            System.Text.StringBuilder s = new System.Text.StringBuilder("");

            s.Append(t1);
            for (int i = startPage; i <= endPage; i++)
            {
                if (i == curPage)
                {
                    s.Append("<span>");
                    s.Append(i);
                    s.Append("</span>");
                }
                else
                {
                    s.Append("<a href=\"/");
                    s.Append(controllerName);
                    s.Append("/");
                    s.Append(i);
                    s.Append("\">");
                    s.Append(i);
                    s.Append("</a>");
                }
            }
            s.Append(t2);
            return s.ToString();
        }
    }
}
